<?
class yiistoUrlRule extends CBaseUrlRule
{
    public $connectionID = 'db';
 
    public function createUrl($manager,$route,$params,$ampersand){
        if ($route==='category/index'){
            $id = $params['id'];
			$category = Category::model()->findbypk($id);
			if($category){
				if($category['parent'] == 0){
					return $category['permalink'];
				}else{
					$category2 = $category;
					$category1 = Category::model()->findbypk($category['parent']);
					if($category1){
						$return = $category1['permalink'].'/'.$category2['permalink'];
						$return .= '/';
						if(isset($params['order'])){
							$return .= 'o'.$params['order'];
						}
						if(isset($params['perpage'])){
							$return .= 'l'.$params['perpage'];
						}
						if(isset($params['page'])){
							$return .= 'p'.$params['page'];
						}
						if(isset($params['manufacturer'])){
							if(count($params['manufacturer'])){
								if(is_array($params['manufacturer'])){
									sort($params['manufacturer']);
									$return .= 'm'.implode(':', $params['manufacturer']);
								}else{
									$return .= 'm'.$params['manufacturer'];
								}
							}
						}
						if(isset($params['feature'])){
							if(count($params['feature'])){
								if(is_array($params['feature'])){
									sort($params['feature']);
									$return .= 'f'.implode(':', $params['feature']);
								}else{
									$return .= 'f'.$params['feature'];
								}
							}
						}
						if(isset($params['price'])){
							$return .= 'r';
							if(isset($params['price']['start'])){
								$return .= $params['price']['start'];
							}
							if(isset($params['price']['end'])){
								$return .= ':'.$params['price']['end'];
							}
						}
						return $return;
					}
				}
			}
        }elseif ($route==='item/index'){
            $id = $params['id'];
			$item = Item::model()->findbypk($id);
			if($item){
				$category2 = Category::model()->findbypk($item['category']);
				if($category2){
					$category1 = Category::model()->findbypk($category2['parent']);
					if($category1){
						return $category1['permalink'].'/'.$category2['permalink'].'/'.$item['permalink'].'.html';
					}
				}
			}else{
				return false;
			}
        }elseif ($route==='manufacturer/single'){
            $id = $params['id'];
			$manufacturer = Manufacturer::model()->findbypk($id);
			if($manufacturer){
				return 'manufacturer/'.$manufacturer['permalink'].'.html';
			}
        }elseif ($route==='manufacturer/index'){
			if(isset($params['page'])){
				return 'manufacturer/p'.$params['page'];
			}else{
				return 'manufacturer';
			}
        }elseif ($route==='page/index'){
            $id = $params['id'];
			$page = Page::model()->findbypk($id);
			if($page){
				return $page['permalink'].'.html';
			}
        }
        return false;
    }
 
    public function parseUrl($manager,$request,$pathInfo,$rawPathInfo){

		/*страница списка производителей*/
	    $re1='(manufacturer)';
	    $re2='(\\/)';
	    $re3='(p)';
	    $re4='(\\d+)';
		if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4."/is", $pathInfo, $matches)){
			$re1='(p)';
			$re2='((?:[0-9-]+))';
			if ($c=preg_match_all ("/".$re1.$re2."/is", $pathInfo, $matches)){
				return '/manufacturer/index/page/'.$matches[2][0];
			}else{
				return '/manufacturer/index';
			}
		}
		
		/*страница производителя*/
		$re1='(manufacturer)';
		$re2='(\\/)';
		$re3='((?:[_a-z0-9-]+))';
		if ($c=preg_match_all ("/".$re1.$re2.$re3."/is", $pathInfo, $matches)){
			$manufacturer = $matches[3][0];
			$manufacturer = Manufacturer::model()->findByAttributes(array('permalink' => $manufacturer));
			if($manufacturer){
				return 'manufacturer/single/id/'.$manufacturer['id'];
			}else{
				return false;
			}
		}
		
		/*статическая страница*/
		$re1='((?:[_a-z0-9-]+))';
		$re2='(\\.)';
		$re3='(html)';
		if ($c=preg_match_all ("/".$re1.$re2.$re3."/is", $pathInfo, $matches)){
			$page = $matches[1][0];
			$page = Page::model()->findByAttributes(array('permalink' => $page));
			if($page){
				return 'page/index/id/1';
			}
		}
		
		/*страница товара*/
		$re1='((?:[_a-z0-9-]+))';
		$re2='\\/';
		$re3='((?:[_a-z0-9-]+))';
		$re4='\\/';
		$re5='((?:[_a-z0-9-]*))';
		$re6='(\\.)';
		$re7='(html)';
		if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6.$re7."/is", $pathInfo, $matches)){
			$item = 		$matches[3][0];
			$category2 = 	$matches[2][0];
			$category1 = 	$matches[1][0];

			$item = Item::model()->findByAttributes(array('permalink' => $item));
			$category2 = Category::model()->findByAttributes(array('permalink' => $category2));
			$category1 = Category::model()->findByAttributes(array('permalink' => $category1));

			if($item){
				if($item['category'] == $category2['id']){
					if($category2['parent'] == $category1['id']){
						return 'item/index/id/'.$item['id'];
					}
				}
			}else{
				return false;
			}
		}
		
		/*страница категории 2 уровня*/
		$re1='((?:[_a-z0-9-]*))';
	    $re2='(\\/)';
	    $re3='((?:[_a-z0-9-]*))';
		if ($c=preg_match_all ("/".$re1.$re2.$re3."/is", $pathInfo, $matches)){
			$category1 = $matches['1']['0'];
			$category1 = Category::model()->findByAttributes(array('permalink' => $category1));
			if($category1){
				$category2 = $matches['3']['0'];
				$category2 = Category::model()->findByAttributes(array('permalink' => $category2));
				if($category2['parent'] == $category1['id']){
					$return = 'category/index/id/'.$category2['id'].'/parent/'.$category1['id'];

                    $re1='(o{1})';
               		$re2='(([1-4]{1}))';
               		if ($c=preg_match_all ("/".$re1.$re2."/is", $pathInfo, $matches)){
               		    $return .= '/order/'.$matches[2][0];
               		}
	  
					$re1='(l{1})';
					$re2='((?:[0-9-]+))';
					if ($c=preg_match_all ("/".$re1.$re2."/is", $pathInfo, $matches)){
						$return .= '/perpage/'.$matches[2][0];
					}
	 
					$re1='(p{1})';
					$re2='((?:[0-9-]+))';
					if ($c=preg_match_all ("/".$re1.$re2."/is", $pathInfo, $matches)){
						$return .= '/page/'.$matches[2][0];
					}
					
					$re1='(m{1})';
					$re2='((?:[0-9-:]+))';
					if ($c=preg_match_all ("/".$re1.$re2."/is", $pathInfo, $matches)){
						$return .= '/manufacturer/'.$matches[2][0];
					}
	  
					$re1='(f{1})';
					$re2='((?:[0-9-:]+))';

					if ($c=preg_match_all ("/".$re1.$re2."/is", $pathInfo, $matches)){
						$return .= '/feature/'.$matches[2][0];
					}

					$re1='(r{1})';
					$re2='.*?';
					$re3='((?:[0-9-:]+))';

					if ($c=preg_match_all ("/".$re1.$re2.$re3."/is", $pathInfo, $matches)){
						$return .= '/pricerange/'.$matches[2][0];
					}

					return $return;
				}else{
					return false;
				}
			}else{
				return false;
			}
		}
		
		/*страница категории 1 уровня*/
	    $re1='((?:[_a-z0-9-]*))';
		if ($c=preg_match_all ("/".$re1."/is", $pathInfo, $matches)){
			$category1 = $matches['1']['0'];
			$category1 = Category::model()->findByAttributes(array('permalink' => $category1, 'parent' => '0'));
			if($category1){
				return 'category/index/id/'.$category1['id'];
			}
		}
		return false;
	}
}	
?>